package com.pig4cloud.pig.module.controller.mer;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.DateUtils;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.Mer;
import com.pig4cloud.pig.module.api.entity.mer.MerSelfVipRechargeRecord;
import com.pig4cloud.pig.module.service.mer.IMerSelfVipRechargeRecordService;
import com.pig4cloud.pig.module.service.mer.IMerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 会员充值记录Controller
 *
 * @author ruoyi
 * @date 2023-06-12
 */
@Validated
@Api(value = "会员充值记录控制器", tags = {"会员充值记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mer/self/vip/rechargeRecord")
public class MerSelfVipRechargeRecordController {

    private final IMerService merService;
    private final IMerSelfVipRechargeRecordService merSelfVipRechargeRecordService;

    /**
     * 查询会员充值记录列表
     */
    @ApiOperation("查询会员充值记录列表")
    @PreAuthorize("@pms.hasPermission('module:vipRechargeRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<MerSelfVipRechargeRecord> list(@Validated MerSelfVipRechargeRecord rechargeRecord) {
        QueryWrapper<MerSelfVipRechargeRecord> qw = new QueryWrapper<>();
        if(rechargeRecord.getSelfVipId()!= null){
            qw.eq("self_vip_id", rechargeRecord.getSelfVipId());
        }
        if(StrUtil.isNotEmpty(rechargeRecord.getBeginDate())){
            qw.between("create_time",rechargeRecord.getBeginDate() + " 00:00:00",rechargeRecord.getEndDate() + " 23:59:59");
        }
        qw.orderByDesc("create_time");
        TableDataInfo<MerSelfVipRechargeRecord> td = PageUtils.buildDataInfo(merSelfVipRechargeRecordService.page(PageUtils.buildPage(),qw));
        List<MerSelfVipRechargeRecord> list = td.getData();
        return td;
    }

    /**
     * 导出会员充值记录列表
     */
    @ApiOperation("导出会员充值记录列表")
    @PreAuthorize("@pms.hasPermission('module:vipRechargeRecord:export')")
    @SysLog("会员充值记录")
    @GetMapping("/export")
    public List export(@Validated MerSelfVipRechargeRecord rechargeRecord) {
        QueryWrapper<MerSelfVipRechargeRecord> qw = new QueryWrapper<>();
        if(rechargeRecord.getSelfVipId()!= null){
            qw.eq("self_vip_id", rechargeRecord.getSelfVipId());
        }
        if(StrUtil.isNotEmpty(rechargeRecord.getBeginDate())){
            qw.between("create_time",rechargeRecord.getBeginDate() + " 00:00:00",rechargeRecord.getEndDate() + " 23:59:59");
        }
        qw.orderByDesc("create_time");
        List<MerSelfVipRechargeRecord> list = merSelfVipRechargeRecordService.list(qw);
        if(CollUtil.isNotEmpty(list)){
            for (MerSelfVipRechargeRecord rechargeRecord1 : list){
                Mer mer = merService.getById(rechargeRecord1.getMerId());
                if(mer!= null){
                    rechargeRecord1.setMerName(mer.getShopName());
                }

                rechargeRecord1.setCreateTimeStr(DateUtils.getDate(rechargeRecord1.getCreateTime()));
            }
        }
        // ExcelUtil<MerSelfVipRechargeRecord> util = new // ExcelUtil<MerSelfVipRechargeRecord>(MerSelfVipRechargeRecord.class);
        return  list;// util.exportExcel(list, "会员充值记录");
    }

    /**
     * 获取会员充值记录详细信息
     */
    @ApiOperation("获取会员充值记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:vipRechargeRecord:query')")
    @GetMapping("/{id}")
    public R<MerSelfVipRechargeRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        MerSelfVipRechargeRecord merSelfVipRechargeRecord = merSelfVipRechargeRecordService.getById(id);
        return R.ok(merSelfVipRechargeRecord);
    }
}
